package ctf.evaluation.SimulatorTests;

import static ctf.model.Side.LEFT;
import static ctf.model.Side.RIGHT;

import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

import ctf.evaluation.Point;
import ctf.evaluation.StandardLocations;
import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.responses.TreesResponse;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;

public class TreeTests extends TestCase{
	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	
	public void setUp() throws Exception {
		ConnectionManager connections = ConnectionManager.instance();
		connections.enterState(ConnectionManager.STEALState.NEW_ROUND);
		
		red = connections.red();
		blue = connections.blue();
	}
	
	
	public void testTreesMaintainPosition() throws IOException {
		
	}
	
	public void testTreeNotInJail() throws IOException, SocketClosedException, UninitializedSocketException {
		red.sendLine("getTrees");		
	}
	
	public void testTreesToClose() throws IOException, NetworkException, ProtocolError {
		// Verify tree placement
		red.sendLine("getTrees");
		
		Point[] trees = (new TreesResponse(red)).trees();
		for(int i = 0; i < 10; i++)
		{			
			for(int j = i + 1; j < 11; j++ ){
				assertTrue(trees[i].distance(trees[j]) > 3);
			}
		}

		blue.sendLine("getTrees");
		trees = (new TreesResponse(blue)).trees();
		for(int i = 0; i < 10; i++)
		{
			for(int j = i + 1; j < 11; j++ ){
				assertTrue(trees[i].distance(trees[j]) > 3);
			}
		}
		
	}
	
	public void testTreesNotOverlappingPlayers() throws IOException, NetworkException, ProtocolError {
		red.sendLine("getTrees");
		
		Point[] trees = (new TreesResponse(red)).trees();
		boolean treeError = false;
		for ( int i = 0; i < 10; i++){
				Point[] positions = StandardLocations.players(LEFT);
				for ( int j = 0; j < positions.length; j++){
					if (positions[j].distance(trees[i]) < 1){
						treeError = true;
					}
				}
		}
		assertFalse(treeError);
		
		treeError = false;
		
		blue.sendLine("getTrees");
		trees = (new TreesResponse(blue)).trees();
		for(int i = 10; i < 20; i++)
		{
			Point[] positions = StandardLocations.players(RIGHT);
			for ( int j = 0; j < positions.length; j++){
				if (positions[j].distance(trees[i]) < 1){
					treeError = true;
				}
			}
		}
		assertFalse(treeError);
	}
	
	//Ensures trees are placed on own side
	public void testTreesOnOwnSide() throws IOException, NetworkException, ProtocolError{
		red.sendLine("getTrees");
		Point[] trees = (new TreesResponse(red)).trees();
		boolean treeError = false;
		for( int i = 0; i < 10; i++){
			Point tempTree = trees[i];
			if (tempTree.x < 1 || tempTree.x > 249 || tempTree.y < 1 || tempTree.y > 249){
				treeError = true;
			}
		}
		assertFalse(treeError);
		
			
		blue.sendLine("getTrees");
		trees = (new TreesResponse(blue)).trees();
		treeError = false;
		for( int i = 10; i < 20; i++){
			Point tempTree = trees[i];
			if (tempTree.x < 251 || tempTree.x > 499 || tempTree.y < 1 || tempTree.y > 249){
				treeError = true;
			}
		}
		assertFalse(treeError);
		
	}
	
}
